/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.fields;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.daikit.commons.shared.utils.DkIdGenerator;
import com.google.gwt.core.client.GWT;

public class ComboObject<O extends Object>
{

	private O object;
	private String string;
	private String id;

	public ComboObject(final O object)
	{
		this(object, object == null ? null : object.toString());
	}

	public ComboObject(final O object, final String toString)
	{
		this.string = toString;
		this.object = object;
		this.id = DkIdGenerator.generateRandomId(12);
	}

	public O getObject()
	{
		return object;
	}

	public String getString()
	{
		return string;
	}

	public String getId()
	{
		return id;
	}

	private static ComboObjectProperties comboObjectProperties;

	public static ComboObjectProperties getProperties()
	{
		if (comboObjectProperties == null)
		{
			comboObjectProperties = GWT.create(ComboObjectProperties.class);
		}
		return comboObjectProperties;
	}

	public static final <O> List<ComboObject<O>> getComboObjects(final List<O> objects)
	{
		final List<ComboObject<O>> comboObjects = new ArrayList<ComboObject<O>>();
		for (final O object : objects)
		{
			comboObjects.add(new ComboObject<O>(object));
		}
		return comboObjects;
	}

	public static final <O> List<ComboObject<O>> getComboObjects(final Map<O, String> objectsMap)
	{
		final List<ComboObject<O>> comboObjects = new ArrayList<ComboObject<O>>();
		for (final Entry<O, String> entry : objectsMap.entrySet())
		{
			comboObjects.add(new ComboObject<O>(entry.getKey(), entry.getValue()));
		}
		return comboObjects;
	}
}
